{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [B1P245表8-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {},
   "outputs": [],
   "source": [
    "L1 = [1,2,3]\n",
    "L2 = [4,5,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1+L2 # 拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "R = L1 * 3 # 重复\n",
    "print(R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n",
      "[1, 9, 3, 1, 9, 3, 1, 9, 3]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 9, 3]"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1[1] = 9\n",
    "print(R)\n",
    "print(L1 * 3)\n",
    "L1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 in L1 # 成员关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 9, 3, 9]"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1.append(9) # 尾部添加\n",
    "L1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 9, 3, 9, 10, 11, 12]"
      ]
     },
     "execution_count": 190,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1.extend([10,11,12]) # 尾部扩展\n",
    "L1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 9, 5, 3, 9, 10, 11, 12]"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# L.insert(i, x) 在 i 处插入元素 x\n",
    "L1.insert(2, 5) # 插入，在位置 2 插入 5\n",
    "L1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# L.index(x) 元素 x 的索引值\n",
    "L1.index(9) # 索引，返回元素在列表里的索引，若含有多个这样的元素，只返回最左边那个的索引,若找不到这样的元素就报错。\n",
    "# 字符串中 str.find(x)，字符 x 第一次出现的位置。若在字符串中找不到 x，会返回值-1，即最右边的位置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# L.count(x) 元素 x 出现的次数\n",
    "L1.count(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 9, 9, 10, 11, 12]"
      ]
     },
     "execution_count": 194,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1.sort() # 返回 None,所以别用 t = L.sort()这样的语句。就地排序，L1已经改变\n",
    "# sorted(L) 返回一个排完序的新列表，不改变原始的 L；[B1P254]\n",
    "# sorted() 适用于任何可迭代容器，list.sort()仅支持list（本身就是list的一个方法）\n",
    "L1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 11, 10, 9, 9, 5, 3, 1]"
      ]
     },
     "execution_count": 195,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1.reverse() # 反转\n",
    "L1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12, 11, 10, 9, 9, 5, 3, 1]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "t = L1.copy() # 复制\n",
    "print(t)\n",
    "t.clear() # 清除\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 11, 10, 9, 9, 3, 1]"
      ]
     },
     "execution_count": 197,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# L.pop(i) 删除索引 i 处的元素，若无 i 值,默认删除最后一个元素\n",
    "L1.pop(5) # 就地改变\n",
    "L1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 10, 9, 9, 3, 1]"
      ]
     },
     "execution_count": 198,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# L.remove(x) 删除元素 x\n",
    "L1.remove(11)\n",
    "L1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 9, 9, 3, 1]"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# del L[i] 删除 i 处元素\n",
    "# del L[i:j] 删除 i 到 j 处的片段\n",
    "# L[i:j] = [] 删除 i 到 j 处的片段\n",
    "# L[i:j] = list_x 分片赋值\n",
    "del L1[1]\n",
    "L1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
